﻿using System;
using System.ComponentModel;

namespace EbbinghausWord.Entities
{
    /// <summary>
    /// 单词实体类
    /// </summary>
    public class WordEntity : INotifyPropertyChanged
    {
        /// <summary>
        /// 英语
        /// </summary>
        public string English { get; set; }

        /// <summary>
        /// 音标
        /// </summary>
        public string Soundmark { get; set; }

        /// <summary>
        /// 中文
        /// </summary>
        public string Chinese { get; set; }

        /// <summary>
        /// 所属单元索引，从1开始
        /// </summary>
        public int UnitIndex { get; set; }


        private RememberStatus _rememberStatus;
        /// <summary>
        /// 记忆状态
        /// </summary>
        public RememberStatus RememberStatus
        {
            get { return _rememberStatus; }
            set
            {
                _rememberStatus = value;
                NotifyChanged("RememberStatus");
            }
        }

        private int _rememberLevel;
        /// <summary>
        /// 记忆级别
        /// </summary>
        public int RememberLevel
        {
            get { return _rememberLevel; }
            set
            {
                _rememberLevel = value;
                NotifyChanged("RememberLevel");
            }
        }

        /// <summary>
        /// 记忆次数，记住3次则算做记住
        /// </summary>
        public int RememberTimes { get; set; }

        /// <summary>
        /// 开始记忆的时间
        /// </summary>
        public DateTime StartRemeberingDateTime { get; set; }

        /// <summary>
        /// 是否在单词本中
        /// </summary>
        public bool IsInTheWordBook { get; set; }

        /// <summary>
        /// 记忆计数器
        /// </summary>
        public int RememberCounter { get; set; }

        /// <summary>
        /// 未记住的计数器
        /// </summary>
        public int NotRememberCounter { get; set; }

        /// <summary>
        /// 随机排序的种子
        /// </summary>
        public string OrderRandom
        {
            get { return Guid.NewGuid().ToString(); }
        }

        /// <summary>
        /// 属性变化事件
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// 通知属性变化
        /// </summary>
        /// <param name="property"></param>
        private void NotifyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

        /// <summary>
        /// 转换成字符串
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return string.Format("{0},{1},{2},{3},{4},{5},{6}", English, IsInTheWordBook ? 1 : 0,
                RememberLevel, StartRemeberingDateTime, RememberCounter, NotRememberCounter, RememberTimes);
        }

        /// <summary>
        /// 从字符串生成单词实体
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static WordEntity CreateEntityFromString(string str)
        {
            var entities = str.Split(',');
            return new WordEntity()
                                 {
                                     English = entities[0].Trim(),
                                     IsInTheWordBook = entities[1] == "1",
                                     RememberLevel = int.Parse(entities[2]),
                                     StartRemeberingDateTime = DateTime.Parse(entities[3]),
                                     RememberCounter = int.Parse(entities[4]),
                                     NotRememberCounter = int.Parse(entities[5]),
                                     RememberTimes = entities.Length == 6 ? 0 : int.Parse(entities[6]),
                                 };
        }
    }

    /// <summary>
    /// 记忆状态
    /// </summary>
    public enum RememberStatus
    {
        /// <summary>
        /// 未记住
        /// </summary>
        NotRemember = 0,
        /// <summary>
        /// 记忆中
        /// </summary>
        Remembering = 1,
        /// <summary>
        /// 已记住
        /// </summary>
        Remembered = 2,
    }
}
